package com.example.rabbitextensionsdemo.config;

import com.example.rabbitextensionsdemo.constant.Constants;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BasicProperties;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.swing.plaf.PanelUI;

@Configuration
public class RabbitMQConfig {
    // 消息确认
    // 1. 自动确认
    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }
    @Bean("ackExchange")
    public DirectExchange ackExchange() {
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).build();
    }
    @Bean("ackBinding")
    public Binding ackBinding(@Qualifier("ackQueue") Queue queue,@Qualifier("ackExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ack");
    }
    // 持久化
    @Bean("presQueue")
    public Queue presQueue() {
//        return QueueBuilder.durable(Constants.PRES_QUEUE).build(); // 队列默认持久化
        return QueueBuilder.nonDurable(Constants.PRES_QUEUE).build();// 队列不持久化
    }
    @Bean("presExchange")
    public DirectExchange presExchange() {
//        return ExchangeBuilder.directExchange(Constants.PRES_EXCHANGE).durable(true).build();// 交换机持久化(默认)
        return ExchangeBuilder.directExchange(Constants.PRES_EXCHANGE).durable(false).build();// 交换机不持久化
    }
    @Bean("presBinding")
    public Binding presBinding(@Qualifier("presQueue") Queue queue, @Qualifier("presExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("pres");
    }
    // 发送确认
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE).build();
    }
    @Bean("confirmExchange")
    public Exchange confirmExchange() {
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE).build();
    }
    @Bean("confirmBinding")
    public Binding confirmBinding(@Qualifier("confirmQueue") Queue queue,@Qualifier("confirmExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("confirm").noargs();
    }
    // 重试机制
    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }
    @Bean("retryExchange")
    public DirectExchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).build();
    }
    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryQueue") Queue queue , @Qualifier("retryExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("retry");
    }
    // TTL 过期时间
    @Bean("ttlQueue")
    public Queue ttlQueue() {
//        return QueueBuilder.durable(Constants.TTL_QUEUE).ttl(10000).build(); // ttl,设置队列过期时间
        return QueueBuilder.durable(Constants.TTL_QUEUE).build();
    }
    @Bean("ttlExchange")
    public DirectExchange ttlExchange() {
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }
    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlQueue") Queue queue, @Qualifier("ttlExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl");
    }

}
